package com.dl.mall.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.dl.mall.annotation.MethodInfo;
import com.dl.mall.annotation.SaAdminCheckRole;
import com.dl.mall.entity.Page;
import com.dl.mall.entity.Product;
import com.dl.mall.entity.ProductSpecs;
import com.dl.mall.service.AsyncService;
import com.dl.mall.service.admin.AdminService;
import com.dl.mall.service.admin.ProductService_Admin;
import com.dl.mall.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author DL
 * @create 2022-3-30 15:56:49
 */
@Slf4j
@RestController()
@RequestMapping("/admin/product")
@SaAdminCheckRole(value = {"super","admin","user"})
@Api(tags="管理员产品controller")
public class ProductController_Admin {

    @Resource
    private ProductService_Admin productService;

    @ApiOperation(value="预热产品",notes = "将所有商品规格加入redis")
    @GetMapping("/preheat")
    public Result preheat(){
        boolean preheat = productService.preheat();
        if (preheat){
            return Result.ok("预热成功");
        }else {
            return Result.no("失败");
        }
    }

    @ApiOperation(value="判断商品是否存在",notes = "添加商品时判断商品是否存在防止重复")
    @GetMapping("/isExist")
    public Result isExistProduct(String productId){
        boolean existProduct = productService.isExistProduct(productId);
        if (!existProduct){
            return Result.ok("不存在，可以添加");
        }else {
            return Result.no("存在，不可以添加");
        }
    }

    @ApiOperation(value="根据 规格id 修改规格库存",notes = "")
    @GetMapping("/modifySpecsStock")
    public Result modifySpecsStock(String specsId, Integer specsStock){
        boolean modifySpecsStock = productService.modifySpecsStock(specsId, specsStock);
        if (modifySpecsStock){
            return Result.ok("成功");
        }else {
            return Result.no("失败");
        }
    }

    @ApiOperation(value="根据 规格id 修改规格状态",notes = "")
    @GetMapping("/modifySpecsState")
    public Result modifySpecsState(String specsId, Integer state){
        boolean modifySpecsState = productService.modifySpecsState(specsId, state);
        if (modifySpecsState){
            return Result.ok("成功");
        }else {
            return Result.no("失败");
        }
    }

    @ApiOperation(value="根据 产品id 修改产品状态",notes = "")
    @GetMapping("/modifyProductState")
    public Result modifyProductState(String productId, Integer state){
        boolean modifyProductState = productService.modifyProductState(productId, state);
        if (modifyProductState){
            return Result.ok("成功");
        }else {
            return Result.no("失败");
        }
    }

/*    *//**
     * 添加产品
     * @param spuJson spu信息
     * @param skuJson sku信息
     * @param mainImg 主图
     * @param subImg 副图
     * @param specsImg 规格图
     * @return 结果
     *//*
    @ApiOperation(value="添加产品",notes = "spuJson=spu信息,skuJson=sku信息,mainImg=主图,subImg=副图,specsImg=规格图")
    @PostMapping("/add")
    public Result productAdd(@Param("spuJson") String spuJson,
                               @Param("skuJson") String skuJson,
                               @Param("mainImg") MultipartFile mainImg,
                               @Param("subImg") MultipartFile[] subImg,
                               @Param("specsImg") MultipartFile[] specsImg){
        long startTime = System.currentTimeMillis();
        boolean addProduct = productService.addProduct(spuJson, skuJson, mainImg, subImg, specsImg);
        long endTime = System.currentTimeMillis();
        log.info("添加商品共耗时：{}ms", endTime - startTime);
        if (addProduct){
            return Result.ok("添加产品成功");
        }else {
            return Result.error("添加产品失败");
        }
    }*/

    @Resource
    private AsyncService asyncService;
    /**
     * 添加产品
     * @param spuJson spu信息
     * @param skuJson sku信息
     * @param mainImg 主图
     * @param subImg 副图
     * @param specsImg 规格图
     * @return 结果
     */
    @ApiOperation(value="添加产品，使用线程池异步执行",notes = "spuJson=spu信息,skuJson=sku信息,mainImg=主图,subImg=副图,specsImg=规格图")
    @PostMapping("/add")
    public Result productAdd(@Param("spuJson") String spuJson,
                             @Param("skuJson") String skuJson,
                             @Param("mainImg") MultipartFile mainImg,
                             @Param("subImg") MultipartFile[] subImg,
                             @Param("specsImg") MultipartFile[] specsImg) throws IOException, ClassNotFoundException {
        //asyncService.asyncUpload(spuJson, skuJson, mainImg, subImg, specsImg);
        boolean addProduct = productService.addProduct(spuJson, skuJson, mainImg, subImg, specsImg);
        return Result.ok("添加产品成功");
    }

    @ApiOperation(value="根据 产品id 查询产品SPU+SKU")
    @MethodInfo
    @GetMapping("/detail")
    public Result getSPUAndSKU(String productId){
        Product spu = productService.getSPU(productId);
        List<ProductSpecs> sku = productService.getSKU(productId);
        if (spu != null && sku != null){
            return Result.ok("查询产品成功").data("spu", spu).data("sku",sku);
        }else {
            return Result.error("查询产品失败");
        }
    }

    @ApiOperation(value="根据 条件 查询产品列表")
    @MethodInfo
    @PostMapping("/list")
    public Result getProductList(@RequestBody JSONObject jsonObject){
        Page<Product> page = productService.getProductListByCondition(jsonObject);

        if (page != null){
            return Result.ok("查询产品成功").data("page", page);
        }else {
            return Result.error("查询产品失败");
        }
    }
}
